package com.cskaoyan.service;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.mapper.CollectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杨杰
 * @version V1.0
 * @Package com.cskaoyan.service
 * @date 2020/12/5 19:27
 * @Copyright © 暂时还没有
 */
@Service
public class AdminCollectServiceImp implements AdminCollectService{

    @Autowired
    CollectMapper collectMapper;

    @Override
    public ListData<Collect> adminCollectList
            (Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {


        PageHelper.startPage(page,limit);

        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(sort+" " + order);
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }

        ListData<Collect> listData = new ListData<>();
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        listData.setItems(collects);
        PageInfo pageInfo = new PageInfo(collects);
        long total = pageInfo.getTotal();
        listData.setTotal((int)total);

        return listData;
    }
}
